﻿using HANS_BackStage.Impl;
using HANS_BackStage.Model.WebModel;
using HANS_BackStageWeb.Common;
using HANS_Base.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1;
using HANS.Utility;
using HANS_Base.Services;

namespace HANS_BackStageWeb.Controllers
{
    public class HomeController : CommonController
    {
        // GET: Home


        public ActionResult test() {
            return View();
        }
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login( string returnUrl) {
            return View(
                AccountServices.SetLoginViewModel(returnUrl)
                );
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginModel"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(HANS_Login loginModel) {
            JsonResultData result = new JsonResultData();
            result.RunWithTry(item=> { 
                    if (!string.IsNullOrEmpty(loginModel.Nane) && !string.IsNullOrEmpty(loginModel.Password))
                    {
                        var msg = AccountServices.Login(loginModel);
                        if (msg.Result)
                        {
                            //return RedirectToRoute(RouteConfig.BackStageDefault, new { controller = "Home", action = "Index" });
                            result.RedirectUrl = Url.Action("Index", "Home");
                            result.AddSuccessMessage(msg.ToString());
                        }
                        else
                        {
                            result.Success = msg.Result;
                            result.AddMessage("账号或密码错误！");
                        }
                    }
            });

            return JsonAllowGet(result); 
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult LoginOut() {
            base.Admin_SignOut();
            HANS_BackStage.BackStageCacheHelper.clearCache();
            return RedirectToAction("Login");
        }
    }
}